#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" This module defines the URL namespace for OCIE.

"""
from functools import partial
from os.path import abspath, dirname, join
from tack.lib import no_cache_app, compose
from tack.lib.urlns import *
from tack_default import api as default_api
from tack_stack.api import site_config


here = abspath(dirname(__file__))
static_dir = join(here, 'static')
htdoc_file = partial(join, static_dir, 'htdoc')
local404 = template_app(htdoc_file('error_404.html'))


def getdsn(env):
    try:
        return env['tack.siteconfig']['stack_dsn']
    except (KeyError, ):
        pass


# our URL DSL
DB = partial(confdbcon, dsn_lookup=getdsn)
FORM = formreader
FORMDB = compose([FORM, DB])
FILES = partial(fileserver, handle404=local404)
NC = no_cache_app
COOKIE = cookie_monster
COOKIEAUTH = default_api.cookie_auth_app
SETROLES = default_api.set_login_roles_app
NEEDROLES = default_api.require_roles_app
ADMINROLES = ['STACK Admin']
USERROLES = ['STACK User']


def GUARD(app, roles):
    return DB(COOKIE(COOKIEAUTH(SETROLES(NEEDROLES(app, roles)))))
    
def FGUARD(app, roles):
    return FORM(DB(COOKIE(COOKIEAUTH(SETROLES(NEEDROLES(app, roles))))))
    
def NCGUARD(app, roles):
    return NC(DB(COOKIE(COOKIEAUTH(SETROLES(NEEDROLES(app, roles))))))


urls = [ 
    ('/api/stack/siteconfig/', 'GET', site_config.site_config_app),
    ('/stack/(?P<fname>.*)',  'GET',  FILES(static_dir)),
]
